﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Platform.Infrastructure;

namespace Platform.Storage.Entity
{

	/// <summary>
	/// Representa o relacionamento entre dois documentos
	/// </summary>
	public class DataObjectRelationship
	{
		protected DataObjectRelationship()
		{ }	// Utilizado pelo EF

		public DataObjectRelationship( string fieldName, string tokenId )
			: this( fieldName, tokenId, null, null )
		{ }

		public DataObjectRelationship( string fieldName, string tokenId, string childEntityName, string childTokenID )
		{
			Arguments.IsNotNull( "fieldName", fieldName );
			Arguments.IsNotNull( "tokenId", childEntityName );

			this.ParentLoweredFieldName = fieldName.ToLowerInvariant();
			this.ParentReversedTokenId = tokenId.Reverse();

			if ( !String.IsNullOrEmpty( childEntityName ) && !String.IsNullOrEmpty( childTokenID ) )
				this.SetReference( childEntityName, childTokenID );
			this.Order = 1;
			this.CreatedOn = DateTime.Now;
		}

		public string ParentLoweredFieldName
		{ get; protected set; }

		public string ParentReversedTokenId
		{ get; protected set; }

		public int Order
		{ get; set; }

		public string ChildLoweredName
		{ get; protected set; }

		public string ChildReversedTokenId
		{ get; protected set; }


		public DateTime CreatedOn
		{ get; protected set; }

		public DateTime UpdatedOn
		{ get; protected set; }

		/// <summary>
		/// Define um relacionamento com a entidade <paramref name="childName"/> e <paramref name="tokenId"/>
		/// </summary>
		/// <param name="childName"></param>
		/// <param name="tokenId"></param>
		public void SetReference( string childName, string tokenId )
		{
			this.ChildLoweredName = childName.ToLowerInvariant();
			this.ChildReversedTokenId = tokenId.Reverse();
		}
	}
}
